﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;

public partial class Admin_Admin_ThemTaiKhoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtCMND.Attributes["onblur"] = "checkCMNDTonTai();";
    }

    //private string createHash(string passwd, string salt)
    //{
    //    HashAlgorithm myHash = new MD5CryptoServiceProvider();
    //    string total = passwd + salt;
    //    byte[] buffer = new byte[total.Length];
    //    buffer = ASCIIEncoding.ASCII.GetBytes(total);
    //    byte[] result = myHash.ComputeHash(buffer);
    //    string pphrase = "";
    //    for (int i = 0; i < result.Length; i++)
    //        pphrase += result[i].ToString("X2");
    //    return pphrase;
    //}

    protected void btnThem_Click(object sender, EventArgs e)
    {
        DataDSTableAdapters.TAIKHOANTableAdapter TKadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        int? kq=0;
        //string matk = txtMaTKNganHang.Text;
        //if (matk == "")
        //    matk = null;
        string salt = DateTime.Now.ToString();
        string hass_pass = GlobalFunctions.createHash(txtPassword.Text, salt);
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        TKadapter.TAIKHOAN_Them(txtCMND.Text, hass_pass, salt, 
            int.Parse(comboboxVaiTro.SelectedValue), txtEmail.Text, txtHoTen.Text,
            txtDiaChi.Text, txtDienThoai.Text,
            imageFolder + fileuploadHinhAnh.FileName,
            chkGuiMail.Checked, null, false , ref kq);


        string strFilename;

        if (fileuploadHinhAnh.FileName != "")
        {
            strFilename = Server.MapPath(imageFolder + fileuploadHinhAnh.FileName);
            fileuploadHinhAnh.PostedFile.SaveAs(strFilename);
     
        }
        if (kq == 0)
            Response.Write("<script>alert('Thêm tài khoản thành công');</script>");
        else
            Response.Write("<script>alert('Thêm tài khoản không thành công');</script>");


    }
}
